package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import module.Content;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import utils.PageUtil;

import domain.vo.ContentQueryObject;
import domain.vo.ContentVO;
import domain.vo.JSONObject;
import domain.vo.Message;

import bussinessService.contentBussinessService;
import bussinessService.impl.ContentBussinessServiceImpl;

@Controller
@RequestMapping("/content")
public class ContentController {
	private Logger logger = Logger.getLogger(ContentController.class.getName());
	/**
	 * 内容业务层实现类
	 */
	private contentBussinessService contentBIS = new ContentBussinessServiceImpl();

	@RequestMapping("/index")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("admin/content/index");
		return mv;
	}
	
	@RequestMapping("/viewList")
	public ModelAndView viewList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("admin/content/viewList");
		return mv;
	}

	@RequestMapping("/viewContentsJson")
	public @ResponseBody
	JSONObject<ContentVO> viewCustomersJson(@RequestParam("page") int page,
			@RequestParam("rows") int rows, HttpSession httpSession) {
		JSONObject<ContentVO> jsonObject = new JSONObject<ContentVO>();
		ContentQueryObject contentQO = new ContentQueryObject();
		contentQO.setType(Content.INDEX);
		try {
			String e_sn = httpSession.getAttribute("e_sn").toString();
			contentQO.setE_sn(e_sn);
			int len = contentBIS.countByQueryObject(contentQO);
			PageUtil.Page(page, rows);
			contentQO.setStart(PageUtil.start);
			contentQO.setEnd(PageUtil.end);
			List<ContentVO> tempVOS = new ArrayList<ContentVO>();
			tempVOS = contentBIS.getObjectByQueryObject(contentQO);
			jsonObject.setTotal(len);
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}

		return jsonObject;
	}
	
	
	@RequestMapping("/addContent")
	public @ResponseBody Message  saveContent(@RequestParam("title") String title,@RequestParam("content") String content,HttpSession session) {
		Message ms = new Message();

		try {
			String e_sn = session.getAttribute("e_sn").toString();
			Content contentObject = new Content();
			contentObject.setContent(content);
			contentObject.setPublisher(e_sn);
			contentObject.setTitle(title);
			contentObject.setType(Content.INDEX);
			contentBIS.merageObject(contentObject);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("内容发布失败");
		}
		return ms;
	}
	
	@RequestMapping("/modifyContent")
	public @ResponseBody Message  modifyContent(@RequestParam("title") String title,@RequestParam("content") String content,@RequestParam("id") int id,HttpSession session) {
		Message ms = new Message();

		try {
			String e_sn = session.getAttribute("e_sn").toString();
			Content contentObject = new Content();
			contentObject.setId(id);
			contentObject.setContent(content);
			contentObject.setPublisher(e_sn);
			contentObject.setTitle(title);
			contentObject.setType(Content.INDEX);
			contentBIS.merageObject(contentObject);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("内容发布失败");
		}
		return ms;
	}	
	

	@RequestMapping("/deleteContent")
	public @ResponseBody Message  deleteContent(@RequestParam("id") int id) {
		Message ms = new Message();
		try {
			contentBIS.deleteT(id);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("删除失败");
		}
		return ms;
	}	
}
